/*************************************************************************
 *                  HONGLING CAPITAL CONFIDENTIAL AND PROPRIETARY
 *
 *                COPYRIGHT (C) HONGLING CAPITAL CORPORATION 2012
 *    ALL RIGHTS RESERVED BY HONGLING CAPITAL CORPORATION. THIS PROGRAM
 * MUST BE USED  SOLELY FOR THE PURPOSE FOR WHICH IT WAS FURNISHED BY
 * HONGLING CAPITAL CORPORATION. NO PART OF THIS PROGRAM MAY BE REPRODUCED
 * OR DISCLOSED TO OTHERS,IN ANY FORM, WITHOUT THE PRIOR WRITTEN
 * PERMISSION OF HONGLING CAPITAL CORPORATION. USE OF COPYRIGHT NOTICE
 * DOES NOT EVIDENCE PUBLICATION OF THE PROGRAM.
 *                  HONGLING CAPITAL CONFIDENTIAL AND PROPRIETARY
 *************************************************************************/

package com.itlong.cloud.utils.page;

import java.util.List;

/**
 * <desc>
 *      page工具类
 * </desc>
 *
 * @date 2016-4-8 09:12
 */
public class PageUtils {
    private int currentPage = 1;//当前页
    private int pageSize = 20;   //页面大小
    private int startSlip;      //起始条数
    private int totalPage;  //总页数
    private int totalNumber; //总条数
    private int skipCount;  //跳过记录数

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(String currentPage) {
        if (currentPage == null || currentPage.equals("")) {
            this.currentPage =1;
        } else {
            this.currentPage =Integer.valueOf(currentPage);
            this.currentPage = this.currentPage > 0 ? this.currentPage : 1;
        }
        this.startSlip = (this.currentPage - 1) * this.pageSize;
        this.skipCount = (this.currentPage - 1) * pageSize;
    }

    public int getStartSlip() {
        return startSlip;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(String pageSize) {
        if(pageSize!=null && !pageSize.equals("")) {
            this.pageSize = Integer.valueOf(pageSize);
        }
        this.startSlip = (this.currentPage - 1) * this.pageSize;
        this.skipCount = (this.currentPage - 1) * this.pageSize;
    }

    public int getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(int totalNumber) {
        this.totalNumber = totalNumber;
        int totalPage = totalNumber % getPageSize() == 0 ? totalNumber / getPageSize() : totalNumber / pageSize + 1;
        this.setTotalPage(totalPage);
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getSkipCount() {
        return skipCount;
    }

    public void setSkipCount(int skipCount) {
        this.skipCount = skipCount;
    }


    /**
     * <p/>
     *      根据list进行分页。
     * <p/>
     *
     * @param {@link java.util.List} list        需要分页的结果集。
     * @return {java.lang.Integer}   currentPage 当前页。
     * @return {java.lang.Integer}   pageSize    页面大小。
     * @return {java.lang.Integer}   totalRecord 总记录数。
     */
    public static <T> List<T> getPageList(
            List<T> list,
            Integer currentPage,
            Integer pageSize,
            Integer totalRecord) {

        //计算总页数
        Integer pageNum = totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1;

        Integer fromIndex = 0; // 从哪里开始截取
        Integer toIndex = 0; // 截取几个
        if (list == null || list.size() == 0)
            return null;
        // 当前页小于或等于总页数时执行
        if (currentPage <= pageNum && currentPage != 0) {
            fromIndex = (currentPage - 1) * pageSize;

            if (currentPage == pageNum) {
                toIndex = list.size();

            } else {
                toIndex = currentPage * pageSize;
            }
        }
        return list.subList(fromIndex, toIndex);
    }
}
